package modulesAPI.v2.composite;
import java.util.GregorianCalendar;
import person.Customer;
import com.caterina.components.CPanel;
import com.caterina.resources.CModules;
import com.caterina.resources.CWords;

import event.AcceptanceState;
import event.Event;
import event.Event.EventType;
import event.Event.PreparationType;
import event.Event.ServiceType;

import budget.Request;
import model.Model;
import modulesAPI.v2.Module;
import modulesAPI.v2.ViewType;
import modulesAPI.v2.basics.ModCalendar;
import modulesAPI.v2.basics.ModTextArea;

public class MRequest extends Module<Request>{
	public MRequest(Module<?> ownerModule, Request content, String tag,String description, ViewType viewType,int...options) {
		super(ownerModule, content, tag, description, viewType,options);
		this.createModules();
		this.addModules(customer,event,date,memo);
	}
	public MRequest (Module<?> ownerModule,Customer customer,String tag,String description, ViewType viewType,int...options){
		this(ownerModule,new Request(
				Model.NO_ID, 
				new GregorianCalendar(), 
				new Event(Model.NO_ID, "", EventType.BIRTHDAY, PreparationType.STANDARD,ServiceType.TABLE, new GregorianCalendar(), new GregorianCalendar(), "", 0, new AcceptanceState()),
				"", 
				customer), 
			 tag, description, viewType, options);
	}
	public MRequest(Module<?> ownerModule,String tag,String description, ViewType viewType,int...options) {
		this(ownerModule, new Request(
				Model.NO_ID, 
				new GregorianCalendar(), 
				new Event(Model.NO_ID, "", EventType.BIRTHDAY, PreparationType.STANDARD,ServiceType.TABLE, new GregorianCalendar(), new GregorianCalendar(), "", 0, new AcceptanceState()),
				"", 
				new Customer("", "", "", "", "", "", "", Customer.Type.PERSON, "",new GregorianCalendar())), 
			tag, description, viewType,options);
	}
	private void createModules(){
		if (this.isOptionEnabled(VIEW_LESS))this.createViewLessModules();
		else if(this.isOptionEnabled(VIEW_MORE_LESS))this.createViewMoreLessModules();
		else this.createStandardView();
		
	}
	private void createStandardView(){
		if (this.isOptionEnabled(CUSTOMER_MODE))
				this.customer=new MCustomer(this, this.getContent().getCustomer(), "REQUEST_CUSTOMER", "REQUEST_CUSTOMER_DESC", ViewType.READ_ONLY,MCustomer.VIEW_MORE_LESS);
		else
				this.customer=new MCustomer(this, this.getContent().getCustomer(), "REQUEST_CUSTOMER", "REQUEST_CUSTOMER_DESC", this.getViewType());
		this.event=new MEvent(this, this.getContent().getEvent(), "REQUEST_EVENT", "REQUEST_EVENT_DESC", this.getViewType());
		this.date = new ModCalendar(this, this.getContent().getRequestDate(), "REQUEST_DATE", "REQUEST_DATE_DESC", ViewType.READ_ONLY);
		this.memo=new ModTextArea(this, this.getContent().getMemo(), "REQUEST_MEMO", "REQUEST_MEMO_DESC", this.getViewType());
	}
	private void createViewLessModules(){
		this.customer=new MCustomer(this, this.getContent().getCustomer(), "REQUEST_CUSTOMER", "REQUEST_CUSTOMER_DESC", ViewType.HIDDEN);
		this.event=new MEvent(this, this.getContent().getEvent(), "REQUEST_EVENT", "REQUEST_EVENT_DESC", this.getViewType(),MEvent.VIEW_LESS);
		this.date = new ModCalendar(this, this.getContent().getRequestDate(),"REQUEST_DATE", "REQUEST_DATE_DESC", ViewType.READ_ONLY);
		this.memo=new ModTextArea(this, this.getContent().getMemo(), "REQUEST_MEMO", "REQUEST_MEMO_DESC", this.getViewType());
	}
	private void createViewMoreLessModules(){
		this.customer=new MCustomer(this, this.getContent().getCustomer(), "REQUEST_CUSTOMER", "REQUEST_CUSTOMER_DESC", ViewType.HIDDEN);
		this.event=new MEvent(this, this.getContent().getEvent(), "REQUEST_EVENT", "REQUEST_EVENT_DESC", this.getViewType(),MEvent.VIEW_MORE_LESS);
		this.date = new ModCalendar(this, this.getContent().getRequestDate(), "REQUEST_DATE", "REQUEST_DATE_DESC", ViewType.HIDDEN);
		this.memo=new ModTextArea(this, this.getContent().getMemo(), "REQUEST_MEMO", "REQUEST_MEMO_DESC", ViewType.HIDDEN);
	}
	@Override
	protected CPanel getReadOnlyPanel(boolean isSelected) {
		return this.getDefaultPanel();
	}
	@Override
	protected CPanel getWritablePanel(boolean isSelected) {
		
		return this.getDefaultPanel();
	}
	@Override
	public CPanel getInListPanel(boolean isSelected) {
		return CModules.formatForList(this.getContent().getEvent().getEventName(), 
				CWords.getWord(this.getContent().getEvent().getEventType().name()),isSelected);
	}
	@Override
	protected CPanel getOneLinePanel(boolean isSelected) {
		return null;
	}
	protected void beforeFireChange(Module<?>mod){
		this.setContent(new Request(
				this.getContent().getId(), 
				this.getContent().getRequestDate(), 
				event.getContent(), 
				memo.getContent(), 
				customer.getContent()));
	}
	@Override
	protected void afterFireChange(Module<?> mod) {}
	public static final int VIEW_LESS=0;
	public static final int VIEW_MORE_LESS=1;
	public static final int CUSTOMER_MODE=2;
	private ModCalendar date;
	private Module<Event> event;
	private Module<String> memo;
	private Module<Customer> customer;
}
